package com.quanyan.authority.service.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.authority.mapper.TbOrganizationMapper;
import com.quanyan.authority.model.db.TbOrganization;
import com.quanyan.authority.model.db.TbOrganizationExample;
import com.quanyan.authority.model.request.ReqMenu;
import com.quanyan.authority.model.request.ReqPage;
import com.quanyan.authority.model.response.RespMenu;
import com.quanyan.authority.service.OrganizationService;
import com.quanyan.common.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/22.
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private TbOrganizationMapper tbOrganizationMapper;


    @Override
    public TbOrganization findById(Integer id) {
        if(id != null )
            return tbOrganizationMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public TbOrganization add(ReqMenu reqBaseParam) {
        TbOrganization tbOrganization = new TbOrganization();
        tbOrganization.setCode(reqBaseParam.getCode());
        tbOrganization.setName(reqBaseParam.getName());
        tbOrganization.setParentId(reqBaseParam.getParentId());
        tbOrganization.setCreateTime(new Date());
        tbOrganization.setCityId(reqBaseParam.getCityId());
        tbOrganization.setUpdateTime(new Date());
        tbOrganization.setIsDelete(Constants.IS_DELETE_FALSE);
        tbOrganization.setFounderId(reqBaseParam.getFounder());
        tbOrganization.setFounderName(reqBaseParam.getFounderName());
        tbOrganizationMapper.insertSelective(tbOrganization);
        return tbOrganization;
    }

    @Override
    public void del(Integer id) {
        if(id != null )
             tbOrganizationMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void update(ReqMenu reqBaseParam) {
        TbOrganization tbOrganization = new TbOrganization();
        tbOrganization.setId(reqBaseParam.getId());
        tbOrganization.setCode(reqBaseParam.getCode());
        tbOrganization.setName(reqBaseParam.getName());
        tbOrganization.setUpdateTime(new Date());
        tbOrganization.setCityId(reqBaseParam.getCityId());
        tbOrganization.setIsDelete(reqBaseParam.getIsDelete().byteValue());
        tbOrganizationMapper.updateByPrimaryKeySelective(tbOrganization);
    }

    @Override
    public Page findAll(ReqPage reqPage, ReqMenu reqBaseParam) {
        return null;
    }


    public List<RespMenu> getOrganizationTree(){
        List<TbOrganization> lst = tbOrganizationMapper.selectByExample(new TbOrganizationExample());
        List<RespMenu>  respList = new ArrayList<>();
        for (TbOrganization o:lst){
            RespMenu m = this.covertToRespUserMenu(o);
            respList.add(m);
        }
        return  respList;
    }



    /**
     * 转tree
     * @param menuList
     * @return
     */
    public List<RespMenu> getMenuTree(List<RespMenu> menuList){
        List<RespMenu> nodeList = new ArrayList<RespMenu>();
        for(RespMenu node1 : menuList){
            boolean mark = false;
            for(RespMenu node2 : menuList){
                if(node1.getParentId() !=null && node1.getParentId().equals(node2.getId())){
                    mark = true;
                    if(node2.getChildren() == null)
                        node2.setChildren(new ArrayList<RespMenu>());
                    node2.getChildren().add(node1);
                    break;
                }
            }
            if(!mark){
                nodeList.add(node1);
            }
        }
        return nodeList;
    }

    /**
     *
     * @param t
     * @return
     */
    public RespMenu covertToRespUserMenu(TbOrganization t){
        RespMenu respMenu = new RespMenu();
        respMenu.setId(t.getId());
        respMenu.setParentId(t.getParentId());
        respMenu.setName(t.getName());
        respMenu.setIsDelete(t.getIsDelete());
        respMenu.setCityId(t.getCityId());
        return respMenu;
    }

}
